﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComLib.ConfigService;
using System.Reflection;
using System.IO;
using LogLib;

namespace PhotoNamingService
{
    public class PhotoNameConfig : IniConfigBase
    {
        /// <summary>
        /// 检查默认的配置文件是否存在，不存在则创建
        /// </summary>
        private string GetDefaultDirectory()
        {
            DriveInfo[] allDirves = DriveInfo.GetDrives();
            if (Directory.Exists("D:\\"))
            {
                return "D:\\";
            }
            if (Directory.Exists("C:\\"))
            {
                return "C:\\";
            }
            throw new Exception("没有找到磁盘根目录D:\\或C:\\");
        }

        public override string ConfigDirectory
        {
            get
            {
                return GetDefaultDirectory();
            }
        }

        public override string ConfigFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_strConfigPath))
                {
                    string className = this.GetType().Name;
                    _strConfigPath = Path.Combine(ConfigDirectory, className + ".ini");
                }
                return _strConfigPath;
            }
        }

        /// <summary>
        /// 命名方式
        /// </summary>
        private string _namingType = "PhotoNamingDefault";
        public string NamingType
        {
            get { return _namingType; }
            set { _namingType = value; }
        }

        /// <summary>
        /// 保存照片时是否允许重名
        /// </summary>
        private bool _enableOverride = false;
        public bool EnableOverride
        {
            get { return _enableOverride; }
            set { _enableOverride = value; }
        }

        /// <summary>
        /// 是否在用户界面上显示照片编号
        /// </summary>
        private bool _showing = false;
        public bool Showing
        {
            get { return _showing; }
            set { _showing = value; }
        }
  
        public override bool Save()
        {
            base.Save();        
            SaveField("EnableOverride", _enableOverride);
            SaveField("NamingType", _namingType);        
            SaveField("Showing", _showing);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _namingType = LoadStringValue("NamingType");
                _enableOverride = LoadBoolenValue("EnableOverride");             
                _showing = LoadBoolenValue("Showing");
            }
            catch (Exception ex)
            {
                _error = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("照片命名选项: ");
            builder.Append("[命名方法]:" + NamingType);      
            builder.Append("; [允许重名]:" + (EnableOverride ? "是" : "否"));
            builder.Append("; [向用户显示编号]:" + (_showing ? "是" : "否"));
            return builder.ToString();
        }
    }
}
